// import ol from 'openlayers';
import VectorLayer from 'ol/layer/Vector'
import VectorSource from 'ol/source/Vector'
import Collection from 'ol/Collection'
import ImageLayer from 'ol/layer/Image.js'
import ImageWMS from 'ol/source/ImageWMS'
import Static from 'ol/source/ImageStatic'
import { config } from './config'
export default {
    // 绘制图层
    Vector(map) {
        const layer = new VectorLayer({
            source: new VectorSource({ features: new Collection(), wrapX: false })
        })
        map.addLayer(layer)
        return layer
    },
    // 边界图层
    GeoJsonVector(map) {
        const layer = new VectorLayer({
            source: new VectorSource({ features: new Collection() })
        })
        map.addLayer(layer)
        return layer
    },
    WMS(param) {
        const { map, url, opacity, params, loadFunc, proj } = param
        const option = {
            url,
            params,
            ratio: 1,
            imageLoadFunction: loadFunc,
            projection: proj || config.systemProj
        }
        const source = new ImageWMS(option)
        const layer = new ImageLayer({ opacity, source })
        if (map) {
            map.addLayer(layer)
        }
        return layer
    },
    updateWMS(param) {
        const { layer, params } = param
        if (layer) {
            // const source = <ol.source.ImageWMS>layer.getSource()
            const source = layer.getSource()
            source.updateParams(params)
        } else {
            console.error('layer is undefined!')
        }
    },
    Static(param) {
        const { map, url, extent: imageExtent, opacity } = param
        const source = this._source(url, imageExtent)
        const layer = new ImageLayer({
            source,
            opacity
        })
        if (map) {
            map.addLayer(layer)
        }
        return layer
    },
    updateStatic(param) {
        const { layer, url, extent: imageExtent } = param
        if (layer) {
            layer.setSource(this._source(url, imageExtent))
        } else {
            console.error('layer is undefined!')
        }
    },
    _source(url, imageExtent) {
        return new Static({
            url,
            imageExtent,
            projection: config.systemProj
        })
    },
    remove(map, layer) {
        if (layer) {
            map.removeLayer(layer)
        }
        return (layer = null)
    },
    undo(layer) {
        const source = layer.getSource()
        const features = source.getFeatures()
        const l = features.length
        if (l > 0) {
            source.removeFeature(features[l - 1])
        }
    },
    clear(layer) {
        if (layer) {
            layer.getSource().clear()
        }
    }
}
// # sourceMappingURL=layer.js.map
